<?php

namespace app\supply\logic;


use think\facade\Db;
use app\common\library\Excel;

class SupplyContactLogic extends BasicLogic
{

    /**
     * @desc     采购条码列表
     * @date     2024/12/8
     * @author   zhangheng
     */
    public function getList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $contract_number = getIsset($params,'contract_number');
        
        $where           = [];
        if($contract_number){
            $where[] = ['contract_number','like',"%{$contract_number}%"];
        } 

        $field = "*";

        $res   = Db::name('supply_contract_basic')
            ->field($field)
            ->where($where)
            ->order('id desc')
            ->paginate($limit);

        $list = $res->items()?:[];

        //用户
        $supply_map = array_column($list,'admin_id');
        $supply_map = array_unique($supply_map);
        $system_admin = Db::name('system_admin')->field('username,id')->where('id','in',$supply_map)->select()->toarray();
        $system_admin = array_column($system_admin,'username','id');

        //供应商
        $supply_map = array_column($list,'supply_id');
        $supply_map = array_unique($supply_map);
        $supply = Db::name('supply')->field('supply_name,id')->where('id','in',$supply_map)->select()->toarray();
        $supply = array_column($supply,'supply_name','id');

        //合同
        $contract_number_map = array_column($list,'contract_number');
        $contract_number_map = array_unique($contract_number_map);
        $contract = Db::name('product_contract')->field('contact_url,batch_id')->where('batch_id','in',$contract_number_map)->select()->toarray();
        $contract = array_column($contract,'contact_url','batch_id');
        
        foreach ($list as &$v) {
            $v['supply_name']  = $supply[$v['supply_id']]??'';
            $v['admin_name']   = $system_admin[$v['admin_id']]??'';
            $v['contact_url']  = $contract[$v['contract_number']]??'';
        }

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc    采购条码详情
     * @date    2024/12/8
     * @author  zhangheng
     */
    public function get_info($params=[]){
        $limit           = getIsset($params,'limit',10);
        $contract_number = getIsset($params,'contract_number');
        if(!$contract_number){
            return  $this->jsonMessage(401,false,'合同号不能为空');
        }
        $sku             = getIsset($params,'sku');
        $where           = [];
        $where[] = ['contract_number','like',"%".$contract_number."%"];
        if($sku){
            $where[] = ['seller_sku','like',"%".$sku."%"];
        }

        $res   = Db::name('supply_purchase_water')
             ->field("*")
            ->where($where)
            ->order('id desc')
            ->paginate($limit);

        $list = $res->items()?:[];

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   import
     * @author zhagnheng
     * @date   2024-12-06
     */
    public function import($url,$adminId){
        $data             = Excel::getExcelData('.' . '/' . $url);
        $excelDatas        = isset($data[0]) ? $data[0] : $data;
        if(!$excelDatas)    return json(DefaultResponse(201, false, '暂无数据!', null));
        
        $inserts = [];
        foreach($excelDatas as $v){
            //判断是否是数字
            $insert = [
                'product_name' => $v['A'],
                'spu' => $v['B'],
                'color' => $v['C'],
                'size' => $v['D'],
                'purchase_nums' => $v['E'],
                'seller_sku' => $v['F'],
                'contract_number' => $v['G'],
                'admin_id' => $adminId,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $inserts[] = $insert;
        }
        if(!empty($inserts)){
            Db::name('supply_purchase_water')->insertAll($inserts);
        }else{
            return json(DefaultResponse(201, false, '暂无导入数据!', null));
        }
        return json(DefaultResponse(200, false, '导入成功!', null));
    }

    
    /**
     * @desc    下载模板
     * @date    2024/12/8
     * @author  zhangheng
     */
    public function export($params=[]){
        $lists = [];
        $header = [];
        $header[] = ['商品名称', 'product_name'];
        $header[] = ['商品SPU', 'spu'];
        $header[] = ['颜色', 'color'];
        $header[] = ['尺码', 'size'];
        $header[] = ['sku采购数量', 'purchase_nums'];
        $header[] = ['SKU', 'seller_sku'];
        $header[] = ['合同编号', 'contract_number'];
        $fileName = "export_SupplierProcurement" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    /**
     * @desc    修改打印状态
     * @date    2024/12/10
     * @author  zhangheng
     */
    public function change_status($params=[]){
        $id     = getIsset($params,'id');
        $status = getIsset($params,'status');
        $res = Db::name('supply_purchase_water')->where('id',$id)->update(['status'=>$status]);
        return  $this->jsonMessage(200,true,'修改成功');
    }

    /**
     * @desc    修改打印状态
     * @date    2024/12/10
     * @author  zhangheng
     */
    public function resert_status($params=[]){
        $id     = getIsset($params,'id');
        $status = getIsset($params,'status');
        Db::name('supply_purchase_water')->where('id',$id)->update(['status'=>1]);
        return  $this->jsonMessage(200,true,'修改成功',null);
    }

    /**
     * @desc    修改添加备注
     * @date    2024/12/10
     * @author  zhangheng
     */
    public function add_remark($params=[]){
        $id     = getIsset($params,'id');
        $remark = getIsset($params,'remark');
        Db::name('supply_contract_basic')->where('id',$id)->update(['remark'=>$remark]);
        return  $this->jsonMessage(200,true,'修改成功',null);
    }






}